package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IAddCompletionOrderCommentView;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationCommentFormanagerView;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationCompletionForManagerView;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationCompletionForSuperView;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.adapter.PublicationCompletionOrderMeidaForSuperAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.bean.PublicationOrderDetailBean;
import com.cloudsigns.factorymanager2.engineering.presenter.AddCompletionOrderCommentPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationCommentFormanagerPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationCompletionForSuperPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationOrderDetailPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PublictationCompletionForManagerPresenter;
import com.cloudsigns.factorymanager2.project.activity.AddCompletionOfEvaluationActivity;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/11.
 */

public class PublicationCompletionOrderForSuperActivity extends Activity implements View.OnClickListener, IPublicationOrderDetailView, IPublicationCompletionForManagerView
        , IPublicationCommentFormanagerView, IPublicationCompletionForSuperView {
    private TextView tv_title;
    private ViewGroup rl_back;
    private final String TAG = PublicationCompletionOrderForSuperActivity.class.getSimpleName();
    private ImageView img_project_select;
    //    private ImageView img_add_evaluation;
    private TextView btn_submit_comment;
    private View rl_evalutaion;
    private TextView btn_confirm_evaluation;
    private boolean isclickable;
    private TextView tv_construction_quality, tv_onstruction_schedule, tv_construction_environment, tv_construction_safe, tv_final_evaluation;
    private TextView tv_project_name, tv_engineering_name;
    private View rl_project;
    private View ll_engineering_info_content;
    private TextView edt_dispath_order_time_colon, edt_completionorder_date, edt_publication_company, edt_publication_responsible;
    private TextView edt_submit_account, edt_submit_date;
    private ListView lv_meidalist;
    private PublicationCompletionOrderMeidaForSuperAdapter publicationCompletionOrderMeidaForSuperAdapter;
    List<MediaBean> mediaBeanList = new ArrayList<>();
    private String quality, schedule, environment, safe, final_evaluation;
    private OrderParentBean orderParentBean;
    private String tag = "";
    /*[ [通用]获取工程部上下刊订单详情 ]*/
    private PublicationOrderDetailPresenter publicationOrderDetailPresenter;
    private PublicationOrderDetailBean publicationOrderDetailBean;
    private LayoutInflater layoutInflater;
    /*[ [项目部]写竣工评语 ]*/
//    private AddCompletionOrderCommentPresenter addCompletionOrderCommentPresenter;
    private PublictationCompletionForManagerPresenter publictationCompletionForManagerPresenter;
    private PublicationCompletionForSuperPresenter publicationCompletionForSuperPresenter;
    private PublicationCommentFormanagerPresenter publicationCommentFormanagerPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_publication_completionorder_for_super);

        publicationOrderDetailPresenter = new PublicationOrderDetailPresenter(this);
        publictationCompletionForManagerPresenter = new PublictationCompletionForManagerPresenter(this);
        publicationCompletionForSuperPresenter = new PublicationCompletionForSuperPresenter(this);
        publicationCommentFormanagerPresenter = new PublicationCommentFormanagerPresenter(this);

        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            tag = mIntent.getStringExtra(ConstantUtil.KEY_1);
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);
        }
        publicationOrderDetailPresenter.getPublicationOrderDetail();
    }

    private void init() {
        tv_title = findViewById(R.id.tv_title);
        rl_back = findViewById(R.id.rl_back);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        rl_project = findViewById(R.id.rl_project);
        btn_submit_comment = findViewById(R.id.btn_submit_comment);
        img_project_select = findViewById(R.id.img_project_select);
//        img_add_evaluation = findViewById(R.id.img_add_evaluation);
        btn_confirm_evaluation = findViewById(R.id.btn_confirm_evaluation);
        ll_engineering_info_content = findViewById(R.id.ll_engineering_info_content);
        edt_dispath_order_time_colon = findViewById(R.id.edt_dispath_order_time_colon);
        edt_completionorder_date = findViewById(R.id.edt_completionorder_date);
        edt_publication_company = findViewById(R.id.edt_publication_company);
        edt_publication_responsible = findViewById(R.id.edt_publication_responsible);
        edt_submit_account = findViewById(R.id.edt_submit_account);
        edt_submit_date = findViewById(R.id.edt_submit_date);
        tv_construction_quality = findViewById(R.id.tv_construction_quality);
        tv_onstruction_schedule = findViewById(R.id.tv_onstruction_schedule);
        tv_construction_environment = findViewById(R.id.tv_construction_environment);
        tv_construction_safe = findViewById(R.id.tv_construction_safe);
        tv_final_evaluation = findViewById(R.id.tv_final_evaluation);
        rl_evalutaion = findViewById(R.id.rl_evalutaion);

        lv_meidalist = findViewById(R.id.lv_meidalist);
        publicationCompletionOrderMeidaForSuperAdapter = new PublicationCompletionOrderMeidaForSuperAdapter(this, TAG);
        publicationCompletionOrderMeidaForSuperAdapter.setData(mediaBeanList);
        lv_meidalist.setAdapter(publicationCompletionOrderMeidaForSuperAdapter);

        tv_title.setText("广告上刊");
        img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        rl_project.setOnClickListener(this);
//        img_add_evaluation.setOnClickListener(this);
        btn_confirm_evaluation.setOnClickListener(this);
        rl_back.setOnClickListener(this);
        btn_submit_comment.setOnClickListener(this);

        layoutInflater = getLayoutInflater();

    }

    private void initData() {
        if (null != publicationOrderDetailBean) {
            tv_project_name.setText(publicationOrderDetailBean.getPublicationContent());
            tv_engineering_name.setText("/" + publicationOrderDetailBean.getRegionName());
            edt_dispath_order_time_colon.setText(DateUtil.getYyyy_Mm_Dd(publicationOrderDetailBean.getSendOrderTime()));
            edt_completionorder_date.setText(DateUtil.getYyyy_Mm_Dd(publicationOrderDetailBean.getCompleteDate()));
            edt_publication_company.setText(publicationOrderDetailBean.getCompanyName());
            edt_publication_responsible.setText(publicationOrderDetailBean.getReceiveAccountName());
            edt_submit_account.setText(publicationOrderDetailBean.getAssignCompleteAccountId() + ":" + publicationOrderDetailBean.getAssignCompleteAccountName());
            edt_submit_date.setText(DateUtil.getYyyy_Mm_Dd(publicationOrderDetailBean.getAssignCompleteOrderTime()));


            List<MediaBean> beanList = publicationOrderDetailBean.getMediaList();
            if (null != beanList) {
                mediaBeanList.clear();
                mediaBeanList.addAll(beanList);
                publicationCompletionOrderMeidaForSuperAdapter.setData(mediaBeanList);
                publicationCompletionOrderMeidaForSuperAdapter.notifyDataSetChanged();
            }

            CommonEnum.CompleteStatus completeStatus = publicationOrderDetailBean.getCompleteStatus();
            if (null != completeStatus) {
                if (completeStatus == CommonEnum.CompleteStatus.WORKING) {
                    btnClickable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                    if (!TextUtils.isEmpty(tag)) {
                        if (tag.equals("CompletionOrderListForSuperActivity")) {
                            btnClickable(false);
                        } else if (tag.equals("SuccessOrderListForMnagerActivity")) {
                            btnClickable(true);
                        }
                    }

                } else if (completeStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                    if (!TextUtils.isEmpty(tag)) {
                        if (tag.equals("CompletionOrderListForSuperActivity")) {
                            btnClickable(true);
                        } else if (tag.equals("SuccessOrderListForMnagerActivity")) {
                            btnClickable(false);
                        }
                    }
                } else if (completeStatus == CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE) {
                    btnClickable(false);
                }
            }

        }

    }

    private void btnClickable(boolean bool) {
        isclickable = bool;
        if (bool) {
            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_long_bg_green);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        } else {

            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_bg_short_gray);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        }
    }


    private void showEngineeringInfoDialog() {
        if (ll_engineering_info_content.getVisibility() == View.VISIBLE) {
            ll_engineering_info_content.setVisibility(View.GONE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_engineering_info_content.setVisibility(View.VISIBLE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }


    private void submitCompletion() {
        if (!TextUtils.isEmpty(tag)) {
            if (tag.equals("CompletionOrderListForSuperActivity")) {
                publicationCompletionForSuperPresenter.publicationCompletionForSuper();
            } else if (tag.equals("SuccessOrderListForMnagerActivity")) {
                publictationCompletionForManagerPresenter.publictationCompletionForManager();
            }
        }

//        addCompletionOrderCommentPresenter.addCompletionOrderComment();
    }

    private void submitComment() {
        publicationCommentFormanagerPresenter.publicationCommentFormanager();
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
            case R.id.rl_project:
                showEngineeringInfoDialog();
                break;
            case R.id.btn_submit_comment:
                submitComment();
//                mIntent = new Intent(PublicationCompletionOrderForSuperActivity.this, AddCompletionOfEvaluationActivity.class);
//                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;
            case R.id.btn_confirm_evaluation:
                if (isclickable)
                    submitCompletion();
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
                    Bundle mBundle = data.getExtras();
                    if (null != mBundle) {

                        quality = mBundle.getString("quality");
                        schedule = mBundle.getString("schedule");
                        environment = mBundle.getString("environment");
                        safe = mBundle.getString("safe");
                        final_evaluation = mBundle.getString("final_evaluation");

                        tv_construction_quality.setText(quality);
                        tv_onstruction_schedule.setText(schedule);
                        tv_construction_environment.setText(environment);
                        tv_construction_safe.setText(safe);
                        tv_final_evaluation.setText(final_evaluation);

                        rl_evalutaion.setVisibility(View.VISIBLE);


                    }

                }
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

//    @Override
//    public String getEvaluateInfo() {
//        JSONObject jsonObj = new JSONObject();
//        try {
//            jsonObj.put("施工质量", quality);
//            jsonObj.put("施工进度", schedule);
//            jsonObj.put("施工环境", environment);
//            jsonObj.put("施工安全", safe);
//            jsonObj.put("综合评价", final_evaluation);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        return jsonObj.toString();
//    }

    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return "";
    }

    @Override
    public void publicationCompletionForSuperSuccess() {
        Toast.makeText(this, "提交成功", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(RESULT_OK, mIntent);
        finish();
    }

    @Override
    public void publicationCompletionForSuperFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getComment() {
        return "管理员竣工";
    }

    @Override
    public void publicationCommentFormanagerSuccess() {
        Toast.makeText(this, "提交评价成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void publicationCommentFormanagerFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void publictationCompletionForManagerSuccess() {
        Toast.makeText(this, "提交成功", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(RESULT_OK, mIntent);
        finish();
    }

    @Override
    public void publictationCompletionForManagerFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void getPublicationOrderDetailSuccessed(PublicationOrderDetailBean publicationOrderBean) {
        publicationOrderDetailBean = publicationOrderBean;
        initData();
    }

    @Override
    public void gePublicationOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
